import asyncio
import threading #线程
import tkinter as tk
from config import Config
from util.Logger import Logger
from ui.MainWindow import MainWindow
from services.AIServiceFactory import AIServiceFactory

# "deepseek": "sk-1fd0a7d970a64de5bd31ab4dc782e4a6",
# "zhipu": "df8b91c6c5d54fd6b51bef7ce0f58aae.ECvfn4U3nbh1k3wj",
# "qwen": "sk-c3fd7382b87a45929666544549514e58"


def async_loop(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()

def main():
    logger=Logger(__name__)
    logger.info("开始启动程序----")
    loop=asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    threading.Thread(target=async_loop,args=(loop,),daemon=True).start()
    try:
        config=Config()
        root=tk.Tk()
        root.title("AI 魔力笔记")
        root.geometry("900x600")
        ai_service=AIServiceFactory.create_service(config,loop)
        logger.info("初始化主窗口组件")
        MainWindow(root,ai_service,config)
        root.mainloop()
    except Exception as e:
        logger.critical(f"程序出现错误，{str(e)}",exc_info=True)
        raise
    finally:
        logger.info("关闭程序")
        logger.cleanup_logs(7)

if __name__ == '__main__':
    main()